import { getCategoryAPI } from '@/apis/category.js'
import { onMounted, ref } from 'vue' 
import { useRoute , onBeforeRouteUpdate} from 'vue-router';

export function useCategory(){
    const categoryData =ref({})
    const route =useRoute()
    const getCategory = async(id = route.params.id)=>{
    const res = await getCategoryAPI(id)
    categoryData.value =res.result
}

onMounted(()=>getCategory())

//监听路由变化，更新数据
onBeforeRouteUpdate((to)=>{
    getCategory(to.params.id)
  })

return{
    categoryData
}
}